#include <linux/fs.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <asm/io.h>

static struct class *_defender_class;
static struct device *_defender_class_dev;

static dev_t _defender_devno;                               //  设备号
static int _defender_major = 231;                           //  主设备ID
static int _defender_minor = 0;                             //  子设备ID
static char* _defender_module_name = "defender_sqlxss";     //  模块名

static int defender_open(struct inode* pInode, struct file* pFile)
{
    printk("defender_open success\n");
    return 0;
}

static ssize_t defender_read(struct file* pFile, char __user* pBuf, size_t cbSize, loff_t* pPos)
{
    int nRet;
    printk("defender_read success\n");
    nRet = copy_to_user(pBuf, "1111", 5);
    printk("\tdefender_read = %d", nRet);
    return 0;
}

static ssize_t defender_write(struct file* pFile, const char __user* pBuf, size_t cbSize, loff_t* pPos)
{
    char szBuffer[128];
    int nRet;
    printk("defender_write success\n");
    nRet = copy_from_user(szBuffer, pBuf, cbSize);
    szBuffer[cbSize] = '\0';
    printk("\tdefender_write = %d : %s\n", nRet, szBuffer);
    return 0;
}

static struct file_operations _defender_fops = {
    .owner = THIS_MODULE,
    .open = defender_open,
    .read = defender_read,
    .write = defender_write,
};

int __init defender_init(void)
{
    int ret;

    printk("defender_init success\n");

    _defender_devno = MKDEV(_defender_major, _defender_minor);

    ret = register_chrdev(_defender_major, _defender_module_name, &_defender_fops);

    _defender_class = class_create(THIS_MODULE, "defender_sqlxss");

    _defender_class_dev = device_create(_defender_class, NULL, _defender_devno, NULL, _defender_module_name);

    return 0;
}

void __exit defender_exit(void)
{
    device_destroy(_defender_class, _defender_devno);

    class_destroy(_defender_class);

    unregister_chrdev(_defender_major, _defender_module_name);

    printk("defender_exit success\n");
}

module_init(defender_init);
module_exit(defender_exit);
MODULE_LICENSE("GPL v2");


